// package org.study.shardingsphere.ds.config;
//
// import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
// import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
// import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
// import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
// import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
// import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
// import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
// import lombok.RequiredArgsConstructor;
// import lombok.extern.slf4j.Slf4j;
// import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.beans.factory.annotation.Qualifier;
// import org.springframework.boot.autoconfigure.AutoConfigureBefore;
// import org.springframework.context.annotation.*;
// import org.springframework.util.ResourceUtils;
//
// import javax.sql.DataSource;
// import java.io.File;
// import java.util.List;
// import java.util.Map;
//
// /**
//  * 动态数据源配置。注意：在shardingsphere-5.3.2及之后版本的配置方式有变化，可以不使用这里的API配置类，而采用 YAML 配置文件。
//  * 在 yaml 中的配置示例如下；
//  * driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
//  * url: jdbc:shardingsphere:classpath:sharding.yaml
//  *
//  * @author Administrator
//  * @date 2021-07-14
//  */
// @Slf4j
// @RequiredArgsConstructor(onConstructor = @__(@Autowired))
// @Configuration
// @AutoConfigureBefore({DynamicDataSourceAutoConfiguration.class})
// public class MyDataSourceConfiguration {
//     private static final String SHARDING_DS = "sharding_ds";
//     private final DynamicDataSourceProperties dynamicDataSourceProperties;
//     private final DefaultDataSourceCreator defaultDataSourceCreator;
//
//     @Bean("shardingSphereDataSource")
//     public DataSource shardingSphereDataSource() throws Exception {
//         File ruleFile = ResourceUtils.getFile("classpath:sharding.yaml");
//         return YamlShardingSphereDataSourceFactory.createDataSource(ruleFile);
//     }
//
//     @Bean
//     public DynamicDataSourceProvider dynamicDataSourceProvider(@Qualifier("shardingSphereDataSource") DataSource shardingSphereDataSource) {
//         Map<String, DataSourceProperty> datasourceMap = dynamicDataSourceProperties.getDatasource();
//         AbstractDataSourceProvider dataSourceProvider = new AbstractDataSourceProvider(defaultDataSourceCreator) {
//             @Override
//             public Map<String, DataSource> loadDataSources() {
//                 Map<String, DataSource> dataSourceMap = createDataSourceMap(datasourceMap);
//                 // 将 sharding-sphere 管理的数据源也交给动态数据源管理
//                 dataSourceMap.put(SHARDING_DS, shardingSphereDataSource);
//                 return dataSourceMap;
//             }
//         };
//         return dataSourceProvider;
//     }
//
//     /**
//      * 将动态数据源设置为首选的。当存在多个数据源时，Spring 自动注入的是首选数据源
//      * 设置为首选数据源后，就可以支持 sharding-sphere 的原生配置方式了
//      */
//     @Primary
//     @Bean
//     public DataSource dataSource(List<DynamicDataSourceProvider> dynamicDataSourceProviderList) {
//         DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(dynamicDataSourceProviderList);
//         // dataSource.setPrimary(SHARDING_DS);
//         dataSource.setPrimary(dynamicDataSourceProperties.getPrimary());
//         dataSource.setStrict(dynamicDataSourceProperties.getStrict());
//         dataSource.setStrategy(dynamicDataSourceProperties.getStrategy());
//         dataSource.setP6spy(dynamicDataSourceProperties.getP6spy());
//         // @DSTransactional //本组件集成了alibaba分布式事务组件seata
//         dataSource.setSeata(dynamicDataSourceProperties.getSeata());
//         return dataSource;
//     }
// }
